package com.author.surface;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.font.FontRenderContext;
import java.awt.font.GlyphVector;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.border.BevelBorder;

import com.author.document.style.StyleChangeNotify;

public class ForegroundColorButton extends JButton implements ActionListener {
	private final int			FG_BUTTON_WIDTH				= 30;						// width
																						// of
																						// the
																						// button
	private final int			FG_BUTTON_HEIGHT			= 30;						// height
																						// of
																						// the
																						// button

	/*
	 * State of the FG color button is the color that is currently being
	 * selected by the user
	 */
	private Color				fgColor						= DEFAULT_FOREGROUND_COLOR;

	/* Font used to render the chars "Fg" over the foreground button */
	private final Font			btnFont						= new Font(
																	"Courier New",
																	Font.BOLD,
																	24);

	/*
	 * X and Y coordinates which will be used to position the "Fg" letters
	 * inside the button
	 */
	private static final int	tx							= 0;
	private static final int	ty							= 20;

	/*
	 * Dimensions of the rectangle drawn at the bottom of the button to indicate
	 * the current foreground color
	 */
	private final int			xLoc						= 0;
	private final int			yLoc						= 27;
	private final int			rWidth						= 32;
	private final int			rHeight						= 5;

	/** Default foreground color is {@value} */
	public static final Color	DEFAULT_FOREGROUND_COLOR	= Color.black;

	public ForegroundColorButton(Action action) {
		super(action);

		/* Set size of the Foreground Color button */
		setSize(new Dimension(FG_BUTTON_WIDTH + 2, FG_BUTTON_HEIGHT + 2));
		setPreferredSize(getSize());
		setEnabled(true);

		/* Set border of the Foreground Color button */
		setBorder(new BevelBorder(BevelBorder.LOWERED));
		setBorderPainted(false);

		/* Set visible properties */
		setToolTipText("Foreground Color");
		setFocusable(false);
		setVisible(true);

		this.addActionListener(this); // respond to user clicks and change
										// Foreground color
	}

	/**
	 * @return The state of the foreground color button
	 */
	public Color getForegroundColor() {
		return this.fgColor;
	}

	/**
	 * Sets the foreground color of the button. This will result in a repaint of
	 * the button so that the rectangle below the letters "Fg" present in the
	 * face of the button gets painted with the appropriate color
	 * 
	 * @param color
	 *            , Foreground color
	 */
	public void setForegroundColor(Color color) {
		this.fgColor = color;
		this.repaint();
	}

	@Override
	public void paint(Graphics g) {
		super.paint(g);

		/* Set rendering hints for anti-aliasing */
		Graphics2D g2d = (Graphics2D) g;
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON);

		/* Create glyphs for the chars to display */
		g2d.setColor(Color.black);
		FontRenderContext frc = g2d.getFontRenderContext();
		GlyphVector glyphs = btnFont.createGlyphVector(frc, "Fg");
		g2d.drawGlyphVector(glyphs, tx, ty);

		/*
		 * Draw rectangle below the chars "Fg" indicating the state of the
		 * button (the currently selected color)
		 */
		if (fgColor != null) {
			g2d.setColor(fgColor);
		}
		g2d.fillRect(xLoc, yLoc, rWidth, rHeight);
	}

	@Override
	public void actionPerformed(ActionEvent ae) {
		if (!(ae.getSource() instanceof JButton)) {
			return;
		}

		/* Show new color in the rectangle below and repaint button */
		Color oldfgColor = fgColor;
		fgColor = JColorChooser.showDialog(Surface.getAppSurface(),
				"Select Foreground Color", fgColor);
		if (oldfgColor != fgColor) {
			repaint(); // repaint widget to reflect new foreground color
			StyleChangeNotify.getStyleChangeNotify().setForegroundColor(
					oldfgColor, fgColor); // Notify style observer
		}
	}
}
